// Copyright © 2020 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import React, { useCallback } from 'react'
import { createSelector } from 'reselect'
import { useSelector } from 'react-redux'
import { get, isEmpty, set } from 'lodash'
import { defineMessages, useIntl } from 'react-intl'
import { useNavigate } from 'react-router-dom'

import Form, { useFormContext } from '@ttn-lw/components/form'
import Select from '@ttn-lw/components/select'
import Checkbox from '@ttn-lw/components/checkbox'
import Input from '@ttn-lw/components/input'
import KeyValueMap from '@ttn-lw/components/key-value-map'
import Radio from '@ttn-lw/components/radio-button'
import UnitInput from '@ttn-lw/components/unit-input'
import Button from '@ttn-lw/components/button'
import Icon, { IconInfoCircle, IconPlus, IconTrash, IconX } from '@ttn-lw/components/icon'
import Notification from '@ttn-lw/components/notification'

import Message from '@ttn-lw/lib/components/message'

import tooltipIds from '@ttn-lw/lib/constants/tooltip-ids'
import sharedMessages from '@ttn-lw/lib/shared-messages'
import PropTypes from '@ttn-lw/lib/prop-types'

import {
  ACTIVATION_MODES,
  FRAME_WIDTH_COUNT,
  fCntWidthEncode,
  fCntWidthDecode,
  parseLorawanMacVersion,
} from '@console/lib/device-utils'
import getDataRate from '@console/lib/data-rate-utils'
import {
  maxDutyCycleOptions,
  pingSlotPeriodicityOptions,
  adrAckLimitOptions,
  adrAckDelayOptions,
} from '@console/lib/mac-settings-utils'

import { selectDataRates } from '@console/store/selectors/configuration'
import { selectMacSettingsProfiles } from '@console/store/selectors/mac-settings-profiles'

const m = defineMessages({
  noMacSettingsProfiles: `You have no MAC settings profiles yet.`,
  noMacSettingsProfilesAction:
    'You have no MAC settings profiles yet. Create one to be able to assign it here.',
  macSettingsProfile: 'MAC settings profile',
})

const MacSettingsSection = props => {
  const {
    activationMode,
    resetsFCnt: initialFCnt,
    initiallyCollapsed,
    lorawanVersion,
    isClassB,
    isClassC,
    bandId,
    appId,
    deviceId,
  } = props

  const navigate = useNavigate()
  const { formatMessage } = useIntl()
  const { values, setFieldValue, setFieldTouched } = useFormContext()

  const { mac_settings } = values
  const macSettingsProfiles = useSelector(state => selectMacSettingsProfiles(state))
  const macSettingsProfilesOptions = React.useMemo(
    () =>
      macSettingsProfiles.map(profile => ({
        value: profile.ids.profile_id,
        label: profile.ids.profile_id,
      })),
    [macSettingsProfiles],
  )
  const alreadySelectedDataRates = Object.keys(mac_settings?.adr?.dynamic?.overrides || [])
  const dataRateOverrideOptions = useSelector(
    createSelector(
      state => selectDataRates(state, bandId, values.lorawan_phy_version),
      dataRates =>
        Object.keys(dataRates).reduce(
          (result, key) =>
            result.concat({
              label: getDataRate({ settings: { data_rate: dataRates[key].rate } }),
              value: `data_rate_${key}`,
            }),
          [],
        ),
    ),
  )
  // Filter out the already selected data rate indices.
  const dataRateFilterOption = useCallback(
    option => !alreadySelectedDataRates.includes(option.value),
    [alreadySelectedDataRates],
  )

  const isNewLorawanVersion = parseLorawanMacVersion(lorawanVersion) >= 110
  const isABP = activationMode === ACTIVATION_MODES.ABP
  const isMulticast = activationMode === ACTIVATION_MODES.MULTICAST
  const isOTAA = activationMode === ACTIVATION_MODES.OTAA
  const isDynamicAdr = mac_settings?.adr && 'dynamic' in mac_settings?.adr
  const isStaticAdr = mac_settings?.adr && 'static' in mac_settings?.adr
  const [resetsFCnt, setResetsFCnt] = React.useState(isABP && initialFCnt)
  const handleResetsFCntChange = React.useCallback(evt => {
    const { checked } = evt.target

    setResetsFCnt(checked)
  }, [])

  const pingPeriodicityRequired = isClassB && (isABP || isMulticast)

  const [isCollapsed, setIsCollapsed] = React.useState(initiallyCollapsed)
  const handleIsCollapsedChange = React.useCallback(() => {
    if (!isCollapsed && pingPeriodicityRequired) {
      // Do not close section if `ping_slot_periodicity` is required.
      return
    }

    setIsCollapsed(isCollapsed => !isCollapsed)
  }, [isCollapsed, pingPeriodicityRequired])

  React.useEffect(() => {
    if (isCollapsed && pingPeriodicityRequired) {
      // Expand section if `ping_slot_periodicity` is required.
      setIsCollapsed(false)
    }
  }, [handleIsCollapsedChange, isABP, isClassB, isCollapsed, isMulticast, pingPeriodicityRequired])

  const adrOverrides = mac_settings?.adr.dynamic?.overrides
  const showEditNbTrans = !values.mac_settings?.adr.dynamic?._use_default_nb_trans
  const defaultNbTransDisabled = !values.mac_settings?.adr.dynamic?._override_nb_trans_defaults
  const addOverride = React.useCallback(() => {
    const newOverride = { _data_rate_index: '', min_nb_trans: '', max_nb_trans: '' }
    setFieldValue(
      'mac_settings.adr.dynamic.overrides',
      adrOverrides
        ? { ...adrOverrides, [`_empty-${Date.now()}`]: newOverride }
        : { [`_empty-${Date.now()}`]: newOverride },
    )
    setFieldTouched('mac_settings.adr.dynamic._overrides', true)
  }, [setFieldValue, adrOverrides, setFieldTouched])
  const handleRemoveButtonClick = useCallback(
    (_, index) => {
      setFieldValue(
        'mac_settings.adr.dynamic.overrides',
        Object.keys(adrOverrides)
          .filter(key => key !== index)
          .reduce((acc, key) => ({ ...acc, [key]: adrOverrides[key] }), {}),
      )
    },
    [adrOverrides, setFieldValue],
  )

  // Define a value setter for the data rate index field which
  // handles setting the object keys correctly, since the index
  // is set as the object key in the API schema.
  // A similar result could be done without pseudo values, purely
  // with decoder/encoder, but it would make error mapping
  // more complex.
  const dataRateValueSetter = useCallback(
    ({ setValues }, { name, value }) => {
      const index = name.split('.').slice(-2)[0] // Would be: data_rate_{x}.
      const oldOverride = get(values, `mac_settings.adr.dynamic.overrides.${index}`, {})
      const overrides = { ...get(values, 'mac_settings.adr.dynamic.overrides', {}) }
      // Empty data rate index objects, are stored with a pseudo key. Remove it.
      delete overrides[index]
      // Move the existing values to the new data rate key.
      overrides[value] = { ...oldOverride, _data_rate_index: value }
      setValues(values => set(values, 'mac_settings.adr.dynamic.overrides', overrides))
    },
    [values],
  )

  const getEncodedAdrModeValue = useCallback(mode => {
    if (mode === 'static') {
      return {
        data_rate_index: 0,
        tx_power_index: 0,
        nb_trans: 1,
      }
    }
    return {}
  }, [])

  const encodeAdrMode = value => ({ [value]: getEncodedAdrModeValue(value) })
  const decodeAdrMode = value => (value !== undefined ? Object.keys(value)[0] : null)

  const handleRemoveMacSettingsProfile = useCallback(
    e => {
      e.preventDefault()
      setFieldValue('mac_settings_profile_ids', null)
    },
    [setFieldValue],
  )

  const hasMacSettingsProfile = !isEmpty(values.mac_settings_profile_ids)

  return (
    <>
      <Form.FieldContainer horizontal className="al-end">
        <Form.Field
          name="mac_settings_profile_ids.profile_id"
          title={m.macSettingsProfile}
          component={Select}
          options={macSettingsProfilesOptions}
          placeholder={sharedMessages.selectMacSettingsProfile}
          noOptionsMessage={() => formatMessage(m.noMacSettingsProfiles)}
          fieldWidth="m"
        />
        <Button
          secondary
          icon={IconX}
          message={sharedMessages.remove}
          onClick={handleRemoveMacSettingsProfile}
          disabled={!hasMacSettingsProfile}
        />
      </Form.FieldContainer>
      {macSettingsProfilesOptions.length === 0 && (
        <Notification
          warning
          content={m.noMacSettingsProfilesAction}
          children={
            <Button
              icon={IconPlus}
              onClick={() =>
                navigate(`/applications/${appId}/mac-settings-profiles/add`, {
                  state: { deviceId },
                })
              }
              message={sharedMessages.createMacSettingsProfile}
              className="mt-cs-m"
              secondary
            />
          }
          small
        />
      )}
      {!hasMacSettingsProfile && (
        <>
          <hr className="mt-cs-xxl mb-cs-m" />
          <Form.CollapseSection
            id="mac-settings"
            title={sharedMessages.customMacSettings}
            initiallyCollapsed={initiallyCollapsed}
            onCollapse={handleIsCollapsedChange}
            isCollapsed={isCollapsed}
          >
            <Form.Field
              title={sharedMessages.frameCounterWidth}
              name="mac_settings.supports_32_bit_f_cnt"
              component={Radio.Group}
              encode={fCntWidthEncode}
              decode={fCntWidthDecode}
              tooltipId={tooltipIds.FRAME_COUNTER_WIDTH}
              horizontal
            >
              <Radio label={sharedMessages['16Bit']} value={FRAME_WIDTH_COUNT.SUPPORTS_16_BIT} />
              <Radio label={sharedMessages['32Bit']} value={FRAME_WIDTH_COUNT.SUPPORTS_32_BIT} />
            </Form.Field>
            {(isABP || isOTAA) && (
              <>
                <Form.FieldContainer horizontal>
                  {!isOTAA && (
                    <Form.Field
                      title={sharedMessages.rx1Delay}
                      type="number"
                      tooltipId={tooltipIds.RX1_DELAY}
                      append={<Message content={sharedMessages.secondsAbbreviated} />}
                      name="mac_settings.rx1_delay"
                      component={Input}
                      min={1}
                      max={15}
                      inputWidth="xs"
                      fieldWidth="xs"
                    />
                  )}
                  {!isMulticast && (
                    <Form.Field
                      title={sharedMessages.desiredRx1DelayTitle}
                      type="number"
                      name="mac_settings.desired_rx1_delay"
                      append={<Message content={sharedMessages.secondsAbbreviated} />}
                      tooltipId={tooltipIds.RX1_DELAY}
                      component={Input}
                      min={1}
                      max={15}
                      inputWidth="xs"
                      fieldWidth="xs"
                    />
                  )}
                </Form.FieldContainer>
                <Form.FieldContainer horizontal>
                  {!isOTAA && (
                    <Form.Field
                      title={sharedMessages.rx1DataRateOffset}
                      type="number"
                      name="mac_settings.rx1_data_rate_offset"
                      inputWidth="xxs"
                      fieldWidth="xs"
                      component={Input}
                      min={0}
                      max={7}
                      tooltipId={tooltipIds.DATA_RATE_OFFSET}
                    />
                  )}
                  {!isMulticast && (
                    <Form.Field
                      title={sharedMessages.desiredRx1DataRateOffsetTitle}
                      type="number"
                      inputWidth="xxs"
                      fieldWidth="xs"
                      name="mac_settings.desired_rx1_data_rate_offset"
                      component={Input}
                      min={0}
                      max={7}
                      tooltipId={tooltipIds.DATA_RATE_OFFSET}
                    />
                  )}
                </Form.FieldContainer>
                {!isOTAA && (
                  <Form.Field
                    label={sharedMessages.resetsFCnt}
                    onChange={handleResetsFCntChange}
                    warning={resetsFCnt ? sharedMessages.resetFCntWarning : undefined}
                    name="mac_settings.resets_f_cnt"
                    tooltipId={tooltipIds.RESETS_F_CNT}
                    component={Checkbox}
                  />
                )}
              </>
            )}
            <Form.FieldContainer horizontal>
              {!isOTAA && (
                <Form.Field
                  title={sharedMessages.rx2DataRateIndexTitle}
                  type="number"
                  name="mac_settings.rx2_data_rate_index"
                  component={Input}
                  min={0}
                  max={15}
                  tooltipId={tooltipIds.RX2_DATA_RATE_INDEX}
                  inputWidth="xxs"
                  fieldWidth="xs"
                />
              )}
              {!isMulticast && (
                <Form.Field
                  title={sharedMessages.desiredRx2DataRateIndexTitle}
                  type="number"
                  name="mac_settings.desired_rx2_data_rate_index"
                  component={Input}
                  min={0}
                  max={15}
                  inputWidth="xxs"
                  tooltipId={tooltipIds.RX2_DATA_RATE_INDEX}
                  fieldWidth="xs"
                />
              )}
            </Form.FieldContainer>
            <Form.FieldContainer horizontal>
              {!isOTAA && (
                <Form.Field
                  type="number"
                  min={100000}
                  step={100}
                  title={sharedMessages.rx2Frequency}
                  name="mac_settings.rx2_frequency"
                  component={UnitInput.Hertz}
                  tooltipId={tooltipIds.RX2_FREQUENCY}
                  fieldWidth="xs"
                />
              )}
              {!isMulticast && (
                <Form.Field
                  type="number"
                  min={100000}
                  step={100}
                  title={sharedMessages.desiredRx2FrequencyTitle}
                  name="mac_settings.desired_rx2_frequency"
                  component={UnitInput.Hertz}
                  tooltipId={tooltipIds.RX2_FREQUENCY}
                  fieldWidth="xs"
                />
              )}
            </Form.FieldContainer>
            <Form.FieldContainer horizontal>
              {!isOTAA && (
                <Form.Field
                  title={sharedMessages.maxDutyCycle}
                  name="mac_settings.max_duty_cycle"
                  component={Select}
                  options={maxDutyCycleOptions}
                  fieldWidth="xs"
                  tooltipId={tooltipIds.MAX_DUTY_CYCLE}
                />
              )}
              {!isMulticast && (
                <Form.Field
                  title={sharedMessages.desiredMaxDutyCycle}
                  name="mac_settings.desired_max_duty_cycle"
                  component={Select}
                  options={maxDutyCycleOptions}
                  fieldWidth="xs"
                  tooltipId={tooltipIds.MAX_DUTY_CYCLE}
                />
              )}
            </Form.FieldContainer>
            <Form.Field
              indexAsKey
              name="mac_settings.factory_preset_frequencies"
              component={KeyValueMap}
              title={sharedMessages.factoryPresetFrequencies}
              description={sharedMessages.factoryPresetFreqDescription}
              addMessage={sharedMessages.freqAdd}
              valuePlaceholder={sharedMessages.frequencyPlaceholder}
              tooltipId={tooltipIds.FACTORY_PRESET_FREQUENCIES}
            />
            {isClassC && (
              <Form.Field
                title={sharedMessages.classCTimeout}
                name="mac_settings.class_c_timeout"
                tooltipId={tooltipIds.CLASS_C_TIMEOUT}
                component={UnitInput.Duration}
                unitSelector={['ms', 's']}
                type="number"
                fieldWidth="xs"
              />
            )}
            {(isClassB || isMulticast) && (
              <>
                <Form.Field
                  title={sharedMessages.classBTimeout}
                  name="mac_settings.class_b_timeout"
                  tooltipId={tooltipIds.CLASS_B_TIMEOUT}
                  component={UnitInput.Duration}
                  unitSelector={['ms', 's']}
                  type="number"
                  fieldWidth="xs"
                />
                <Form.Field
                  title={sharedMessages.pingSlotPeriodicity}
                  description={sharedMessages.pingSlotPeriodicityDescription}
                  name="mac_settings.ping_slot_periodicity"
                  component={Select}
                  options={pingSlotPeriodicityOptions}
                  required={pingPeriodicityRequired}
                  menuPlacement="top"
                  fieldWidth="xs"
                />
                <Form.FieldContainer horizontal>
                  {!isOTAA && (
                    <Form.Field
                      type="number"
                      min={100000}
                      title={sharedMessages.beaconFrequency}
                      placeholder={sharedMessages.frequencyPlaceholder}
                      name="mac_settings.beacon_frequency"
                      tooltipId={tooltipIds.BEACON_FREQUENCY}
                      component={UnitInput.Hertz}
                      fieldWidth="xs"
                    />
                  )}
                  {!isMulticast && (
                    <Form.Field
                      type="number"
                      min={100000}
                      title={sharedMessages.desiredBeaconFrequency}
                      placeholder={sharedMessages.frequencyPlaceholder}
                      name="mac_settings.desired_beacon_frequency"
                      tooltipId={tooltipIds.BEACON_FREQUENCY}
                      component={UnitInput.Hertz}
                      fieldWidth="xs"
                    />
                  )}
                </Form.FieldContainer>
                <Form.FieldContainer horizontal>
                  {!isOTAA && (
                    <Form.Field
                      type="number"
                      min={100000}
                      step={100}
                      title={sharedMessages.pingSlotFrequency}
                      placeholder={sharedMessages.frequencyPlaceholder}
                      name="mac_settings.ping_slot_frequency"
                      tooltipId={tooltipIds.PING_SLOT_FREQUENCY}
                      component={UnitInput.Hertz}
                      fieldWidth="xs"
                    />
                  )}
                  {!isMulticast && (
                    <Form.Field
                      type="number"
                      min={100000}
                      step={100}
                      title={sharedMessages.desiredPingSlotFrequencyTitle}
                      placeholder={sharedMessages.frequencyPlaceholder}
                      name="mac_settings.desired_ping_slot_frequency"
                      tooltipId={tooltipIds.PING_SLOT_FREQUENCY}
                      component={UnitInput.Hertz}
                      fieldWidth="xs"
                    />
                  )}
                </Form.FieldContainer>
                <Form.FieldContainer horizontal>
                  {!isOTAA && (
                    <Form.Field
                      title={sharedMessages.pingSlotDataRateTitle}
                      name="mac_settings.ping_slot_data_rate_index"
                      tooltipId={tooltipIds.PING_SLOT_DATA_RATE_INDEX}
                      component={Input}
                      type="number"
                      inputWidth="xxs"
                      fieldWidth="xs"
                      min={0}
                      max={15}
                    />
                  )}
                  {!isMulticast && (
                    <Form.Field
                      title={sharedMessages.desiredPingSlotDataRateTitle}
                      name="mac_settings.desired_ping_slot_data_rate_index"
                      tooltipId={tooltipIds.PING_SLOT_DATA_RATE_INDEX}
                      component={Input}
                      type="number"
                      fieldWidth="xs"
                      inputWidth="xxs"
                      min={0}
                      max={15}
                    />
                  )}
                </Form.FieldContainer>
              </>
            )}
            <Form.FieldContainer horizontal>
              <Form.Field
                title={sharedMessages.statusCountPeriodicity}
                name="mac_settings.status_count_periodicity"
                component={Input}
                append={<Message content={sharedMessages.messages} />}
                type="number"
                inputWidth="s"
                fieldWidth="xs"
                tooltipId={tooltipIds.STATUS_COUNT_PERIODICITY}
              />
              <Form.Field
                title={sharedMessages.statusTimePeriodicity}
                name="mac_settings.status_time_periodicity"
                component={UnitInput.Duration}
                unitSelector={['ms', 's']}
                type="number"
                tooltipId={tooltipIds.STATUS_TIME_PERIODICITY}
                fieldWidth="xs"
              />
            </Form.FieldContainer>
            <Form.Field
              name="mac_settings.adr"
              component={Radio.Group}
              title={sharedMessages.adrAdaptiveDataRate}
              tooltipId={tooltipIds.ADR_USE}
              encode={encodeAdrMode}
              decode={decodeAdrMode}
            >
              <Radio label={sharedMessages.adrDynamic} value="dynamic" />
              <Radio label={sharedMessages.adrStatic} value="static" />
              <Radio label={sharedMessages.disabled} value="disabled" />
            </Form.Field>
            {isDynamicAdr && (
              <>
                <Form.Field
                  title={sharedMessages.adrMargin}
                  name="mac_settings.adr.dynamic.margin"
                  component={Input}
                  type="number"
                  tooltipId={tooltipIds.ADR_MARGIN}
                  min={-100}
                  max={100}
                  inputWidth="xs"
                  append="dB"
                />
                <Form.Field
                  label={sharedMessages.useDefaultNbTrans}
                  name="mac_settings.adr.dynamic._use_default_nb_trans"
                  component={Checkbox}
                  tooltipId={tooltipIds.USE_DEFAULT_NB_TRANS}
                />
                {showEditNbTrans && (
                  <>
                    <Form.Field
                      title={sharedMessages.adrNbTrans}
                      name="mac_settings.adr.dynamic._override_nb_trans_defaults"
                      component={Checkbox}
                      label={sharedMessages.overrideNbTrans}
                    />
                    <Form.FieldContainer horizontal className="al-end mb-cs-xs">
                      <Form.Field
                        title={sharedMessages.minNbTrans}
                        name="mac_settings.adr.dynamic.min_nb_trans"
                        component={Input}
                        type="number"
                        min={1}
                        max={3}
                        disabled={defaultNbTransDisabled}
                        inputWidth="xs"
                        className="d-flex direction-column"
                      />
                      <Form.Field
                        title={sharedMessages.maxNbTrans}
                        name="mac_settings.adr.dynamic.max_nb_trans"
                        component={Input}
                        type="number"
                        min={1}
                        max={3}
                        disabled={defaultNbTransDisabled}
                        inputWidth="xs"
                        className="d-flex direction-column"
                      />
                      <Message content={sharedMessages.defaultForAllRates} className="mt-cs-xl" />
                    </Form.FieldContainer>
                    {!defaultNbTransDisabled && (
                      <div className="d-flex al-center c-text-neutral-semilight">
                        <Icon icon={IconInfoCircle} className="mr-cs-xxs" />
                        <Message content={sharedMessages.defaultNbTransMessage} />
                      </div>
                    )}
                    <Form.InfoField
                      title={sharedMessages.specificOverrides}
                      tooltipId={tooltipIds.DATA_RATE_SPECIFIC_OVERRIDES}
                      className="mt-cs-m"
                    >
                      {adrOverrides &&
                        Object.keys(adrOverrides).map(index => (
                          <Form.FieldContainer horizontal className="al-end" key={index}>
                            <Form.Field
                              title={sharedMessages.dataRatePlaceholder}
                              name={`mac_settings.adr.dynamic.overrides.${index}._data_rate_index`}
                              valueSetter={dataRateValueSetter}
                              component={Select}
                              options={dataRateOverrideOptions}
                              filterOption={dataRateFilterOption}
                              fieldWidth="quarter"
                              className="d-flex direction-column"
                            />
                            <Form.Field
                              title={sharedMessages.minNbTrans}
                              name={`mac_settings.adr.dynamic.overrides.${index}.min_nb_trans`}
                              component={Input}
                              className="d-flex direction-column"
                              type="number"
                              min={1}
                              max={3}
                            />
                            <Form.Field
                              title={sharedMessages.maxNbTrans}
                              name={`mac_settings.adr.dynamic.overrides.${index}.max_nb_trans`}
                              component={Input}
                              className="d-flex direction-column"
                              type="number"
                              min={1}
                              max={3}
                            />
                            <Button
                              type="button"
                              onClick={handleRemoveButtonClick}
                              icon={IconTrash}
                              message={sharedMessages.remove}
                              value={index}
                              secondary
                              danger
                            />
                          </Form.FieldContainer>
                        ))}
                      <Button
                        type="button"
                        message={sharedMessages.addSpecificOverride}
                        onClick={addOverride}
                        icon={IconPlus}
                        secondary
                      />
                    </Form.InfoField>
                  </>
                )}
              </>
            )}
            {isStaticAdr && (
              <>
                <Form.Field
                  title={sharedMessages.adrDataRate}
                  name="mac_settings.adr.static.data_rate_index"
                  component={Input}
                  type="number"
                  inputWidth="xs"
                  min={0}
                />
                <Form.Field
                  title={sharedMessages.adrTransPower}
                  name="mac_settings.adr.static.tx_power_index"
                  component={Input}
                  type="number"
                  inputWidth="xs"
                  max={15}
                />
                <Form.Field
                  title={sharedMessages.adrUplinks}
                  name="mac_settings.adr.static.nb_trans"
                  component={Input}
                  type="number"
                  inputWidth="xs"
                  min={1}
                  max={15}
                />
              </>
            )}
            {isNewLorawanVersion && !isMulticast && (
              <>
                <Form.Field
                  title={sharedMessages.desiredAdrAckLimit}
                  name="mac_settings.desired_adr_ack_limit_exponent"
                  component={Select}
                  options={adrAckLimitOptions}
                  tooltipId={tooltipIds.ADR_ACK_LIMIT}
                  fieldWidth="xs"
                />
                <Form.Field
                  title={sharedMessages.desiredAdrAckDelay}
                  name="mac_settings.desired_adr_ack_delay_exponent"
                  component={Select}
                  options={adrAckDelayOptions}
                  tooltipId={tooltipIds.ADR_ACK_DELAY}
                  fieldWidth="xs"
                />
              </>
            )}
          </Form.CollapseSection>
        </>
      )}
    </>
  )
}

MacSettingsSection.propTypes = {
  activationMode: PropTypes.oneOf(Object.values(ACTIVATION_MODES)).isRequired,
  appId: PropTypes.string.isRequired,
  bandId: PropTypes.string.isRequired,
  deviceId: PropTypes.string.isRequired,
  initiallyCollapsed: PropTypes.bool,
  isClassB: PropTypes.bool,
  isClassC: PropTypes.bool,
  lorawanVersion: PropTypes.string.isRequired,
  resetsFCnt: PropTypes.bool,
}

MacSettingsSection.defaultProps = {
  resetsFCnt: false,
  initiallyCollapsed: true,
  isClassB: false,
  isClassC: false,
}

export default MacSettingsSection
